/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladoresJpa;

import controladoresJpa.exceptions.NonexistentEntityException;
import controladoresJpa.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import model.AssociacaoTabelaAtributo;
import model.Tabela;
import model.AtributoMR;

/**
 * Módulo MR
 * Classe de Entidade do Banco de Dados, gerada automaticamente, porém com certas modificações
 * @author Eduardo, Maylon e Kamilla
 */
public class AssociacaoTabelaAtributoJpaController implements Serializable {

    public AssociacaoTabelaAtributoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;
public AssociacaoTabelaAtributoJpaController(){
    if(emf==null)
        emf = Persistence.createEntityManagerFactory("unb-gama-threedsPU");
            }
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(AssociacaoTabelaAtributo associacaoTabelaAtributo) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tabela nometabela = associacaoTabelaAtributo.getNometabela();
            if (nometabela != null) {
                nometabela = em.getReference(nometabela.getClass(), nometabela.getNome());
                associacaoTabelaAtributo.setNometabela(nometabela);
            }
            AtributoMR nomeatributo = associacaoTabelaAtributo.getNomeatributo();
            if (nomeatributo != null) {
                nomeatributo = em.getReference(nomeatributo.getClass(), nomeatributo.getNome());
                associacaoTabelaAtributo.setNomeatributo(nomeatributo);
            }
            em.persist(associacaoTabelaAtributo);
            if (nometabela != null) {
                nometabela.getAssociacaoTabelaAtributoList().add(associacaoTabelaAtributo);
                nometabela = em.merge(nometabela);
            }
            if (nomeatributo != null) {
                nomeatributo.getAssociacaoTabelaAtributoList().add(associacaoTabelaAtributo);
                nomeatributo = em.merge(nomeatributo);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAssociacaoTabelaAtributo(associacaoTabelaAtributo.getCodigo()) != null) {
                throw new PreexistingEntityException("AssociacaoTabelaAtributo " + associacaoTabelaAtributo + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(AssociacaoTabelaAtributo associacaoTabelaAtributo) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            AssociacaoTabelaAtributo persistentAssociacaoTabelaAtributo = em.find(AssociacaoTabelaAtributo.class, associacaoTabelaAtributo.getCodigo());
            Tabela nometabelaOld = persistentAssociacaoTabelaAtributo.getNometabela();
            Tabela nometabelaNew = associacaoTabelaAtributo.getNometabela();
            AtributoMR nomeatributoOld = persistentAssociacaoTabelaAtributo.getNomeatributo();
            AtributoMR nomeatributoNew = associacaoTabelaAtributo.getNomeatributo();
            
            if (nometabelaNew != null) {
                nometabelaNew = em.getReference(nometabelaNew.getClass(), nometabelaNew.getNome());
                associacaoTabelaAtributo.setNometabela(nometabelaNew);
            }
            if (nomeatributoNew != null) {
                nomeatributoNew = em.getReference(nomeatributoNew.getClass(), nomeatributoNew.getNome());
                associacaoTabelaAtributo.setNomeatributo(nomeatributoNew);
            }
            associacaoTabelaAtributo = em.merge(associacaoTabelaAtributo);
            if (nometabelaOld != null && !nometabelaOld.equals(nometabelaNew)) {
                nometabelaOld.getAssociacaoTabelaAtributoList().remove(associacaoTabelaAtributo);
                nometabelaOld = em.merge(nometabelaOld);
            }
            if (nometabelaNew != null && !nometabelaNew.equals(nometabelaOld)) {
                nometabelaNew.getAssociacaoTabelaAtributoList().add(associacaoTabelaAtributo);
                nometabelaNew = em.merge(nometabelaNew);
            }
            if (nomeatributoOld != null && !nomeatributoOld.equals(nomeatributoNew)) {
                nomeatributoOld.getAssociacaoTabelaAtributoList().remove(associacaoTabelaAtributo);
                nomeatributoOld = em.merge(nomeatributoOld);
            }
            if (nomeatributoNew != null && !nomeatributoNew.equals(nomeatributoOld)) {
                nomeatributoNew.getAssociacaoTabelaAtributoList().add(associacaoTabelaAtributo);
                nomeatributoNew = em.merge(nomeatributoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = associacaoTabelaAtributo.getCodigo();
                if (findAssociacaoTabelaAtributo(id) == null) {
                    throw new NonexistentEntityException("The associacaoTabelaAtributo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            AssociacaoTabelaAtributo associacaoTabelaAtributo;
            try {
                associacaoTabelaAtributo = em.getReference(AssociacaoTabelaAtributo.class, id);
                associacaoTabelaAtributo.getCodigo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The associacaoTabelaAtributo with id " + id + " no longer exists.", enfe);
            }
            Tabela nometabela = associacaoTabelaAtributo.getNometabela();
            if (nometabela != null) {
                nometabela.getAssociacaoTabelaAtributoList().remove(associacaoTabelaAtributo);
                nometabela = em.merge(nometabela);
            }
            AtributoMR nomeatributo = associacaoTabelaAtributo.getNomeatributo();
            if (nomeatributo != null) {
                nomeatributo.getAssociacaoTabelaAtributoList().remove(associacaoTabelaAtributo);
                nomeatributo = em.merge(nomeatributo);
            }
            em.remove(associacaoTabelaAtributo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<AssociacaoTabelaAtributo> findAssociacaoTabelaAtributoEntities() {
        return findAssociacaoTabelaAtributoEntities(true, -1, -1);
    }

    public List<AssociacaoTabelaAtributo> findAssociacaoTabelaAtributoEntities(int maxResults, int firstResult) {
        return findAssociacaoTabelaAtributoEntities(false, maxResults, firstResult);
    }

    private List<AssociacaoTabelaAtributo> findAssociacaoTabelaAtributoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(AssociacaoTabelaAtributo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public AssociacaoTabelaAtributo findAssociacaoTabelaAtributo(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(AssociacaoTabelaAtributo.class, id);
        } finally {
            em.close();
        }
    }

    public int getAssociacaoTabelaAtributoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<AssociacaoTabelaAtributo> rt = cq.from(AssociacaoTabelaAtributo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
